package com.gitee.wsl.lifecycle.nav

import com.gitee.wsl.concurrent.api.cancel.Cancellable
import com.gitee.wsl.data.page.PagesNavigation

interface Navigation<O>: Navigator<O>, NavigationSource<Navigation.Event<O>> {
    class Event<O>(
        val transformer: (O) -> O,
        val onComplete: (newPages: O, oldPages: O) -> Unit = { _, _ -> },
    )
}

/**
 * Returns a default implementation of [PagesNavigation].
 * Broadcasts navigation events to all subscribed observers.
 */
fun <V : Any,R:SelectAbleList<V,R>> defaultNavigation() = DefaultNavigation<R>()


open class DefaultNavigation<O> : Navigation<O> {

    private val relay = Relay<Navigation.Event<O>>()

    override fun subscribe(observer: (Navigation.Event<O>) -> Unit): Cancellable =
        relay.subscribe(observer)

    override fun navigate(transformer: (O) -> O, onComplete: (newPages: O, oldPages: O) -> Unit) {
        relay.accept(Navigation.Event(transformer, onComplete))
    }
}